# app/core/exceptions.py
from typing import Any, Dict, Optional
from app.core.error_code import ErrorCode


class AppException(Exception):
    """应用基础异常类"""

    def __init__(
            self,
            error_code: ErrorCode,
            detail: Optional[str] = None,
            extra_data: Optional[Dict[str, Any]] = None
    ):
        self.error_code = error_code
        self.detail = detail
        self.extra_data = extra_data or {}
        self.messages = f"{[self.error_code.code]} {self.error_code.message}, Details:\n {self.detail}" if self.detail else \
            f"{[self.error_code.code]} {self.error_code.message}"
        super().__init__(self.messages)

    @property
    def code(self):
        """便捷属性：直接访问错误码"""
        return self.error_code.code

    @property
    def error(self):
        """便捷属性：访问基础错误消息"""
        return self.error_code.message


    def to_dict(self) -> Dict[str, Any]:
        """转换为API响应格式"""
        result = {
            "success": False,
            "error_code": self.error_code.code,
            "error_message": self.messages
        }
        if self.extra_data:
            result.update({"extra_data": self.extra_data})
        return result

class ServerException(AppException):
    ...

class ParamikoException(AppException):
    pass

# 具体业务异常类
class RobotException(AppException):
    """机器人相关异常"""
    ...

class TestingDataException(AppException):
    """测试数据相关异常"""
    ...

class ValidationException(AppException):
    """参数验证异常"""
    ...


class DatabaseException(AppException):
    """数据库操作异常"""
    ...

class ScanGatewayException(AppException):
    """获取网关失败"""
    ...

class MockDataErrorException(AppException):
    """mock数据里的失败"""
    ...

class FlexHTTPClientException(AppException):
    """flex http通信失败"""
    ...

class FlexHandlerException(AppException):
    """flex http通信失败"""
    ...

